import type { AxiosResponse } from 'axios'

export const responseDownload = (response: AxiosResponse) => {
  const disposition = response.headers['content-disposition']
  console.log(disposition)
  const filenameRegex = /filename=(.+)/
  const matches = filenameRegex.exec(disposition)
  console.log('下载文件名：', matches)
  const urlCreator = window.URL || window.webkitURL
  const downloadUrl = urlCreator.createObjectURL(response.data)

  const link = document.createElement('a')
  link.href = downloadUrl
  link.setAttribute('download', decodeURI(matches![1].trim()))
  document.body.appendChild(link)
  link.click()
  document.body.removeChild(link)
}
